package com.android.nextBus.JSON;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class StopParser {
	
	/**
	 * Parses the array of stopTime items returned from the server's stop service
	 * into a list of TripTimesItem objects
	 * @param jsonArray the array returned from the Route Service
	 * @return a list of Stop Items
	 */
	public static List<TripsTimesItem> parse(String data, int stopID) {
		
		List<TripsTimesItem> ttItems = new ArrayList<TripsTimesItem>();
		try {
			JSONArray jsonArray = new JSONArray(data);
			
			for (int i = 0; i < jsonArray.length(); ++i) {
				JSONObject obj = (JSONObject) jsonArray.get(i);
				int tripID = obj.getInt("t");
				int shapeID = obj.getInt("s");
				int routeID = obj.getInt("r");
				int departureTime = obj.getInt("d");		 
				String headsign = obj.getString("h");		
				
				// add colon into departure time string
				//departureTime = departureTime.substring(0,2) + ":" + departureTime.substring(2);
					
				ttItems.add(new TripsTimesItem(headsign, shapeID, tripID, 
						stopID, departureTime, routeID));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return ttItems;
	}
}
